package com.handinfo.android.ui.window;

import java.io.DataInputStream;
import java.io.IOException;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.game.Role;
import com.handinfo.android.ui.IUIWindows;
import com.handinfo.android.ui.UIWindows;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.uicontrols.DWListener;
import com.handinfo.android.uicontrols.controls.DWMessageBox;
import com.handinfo.android.utils.Tools;

/**
 * 
 * @author xiaoyingying
 * 
 * 复活界面
 * 
 */
public class UIReliveWindow implements IUIWindows
{
	private static final byte RELIVE_NORMAL = 0;//死亡状态
	private static final byte RELIVE_BUY = 1;//原地复活购买
	private static final byte RELIVE_OTHER = 3;//其他人复活

	public void init()
	{
	}

	private DWMessageBox m_relive_relive;

	private String title = "提示";
	private String[][] menus = {
			{ "回城复活" },
			{ "原地复活", "回城复活" }
	};
	private String[] message = {
			"很不幸，您已经死亡！多种方式可以提升您的实力：<mark c=FF0000>炼魂</mark>打通魂位；<mark c=FF0000>天工坊</mark>强化装备镶嵌灵石；<mark c=FF0000>祭天</mark>台获取天命；升级<mark c=FF0000>技能</mark>等级；<mark c=FF0000>凝练</mark>属性内丹。",
			"原地满血复活需要",
			"元宝你确定?",
			"复活了您，是否接受？"

	};
	private byte m_relive_state = 0;
	private byte m_relive_type = 0;
	private String m_relive_name;
	private int m_relive_gold = 0;
	private String m_relive_message;

	public void open(byte index)
	{
		m_relive_state = index;

		if (DWControlsManager.getInstance().contains(m_relive_relive))
			DWControlsManager.getInstance().removeControl(m_relive_relive);

		switch (m_relive_state) {
			case RELIVE_NORMAL:
				m_relive_relive = new DWMessageBox(title, m_relive_message, menus[m_relive_type][0],
						menus[m_relive_type].length > 1 ? menus[m_relive_type][1] : null);
				m_relive_relive.addListener(new DWListener() {
					public void OnDoubleClick()
					{
					}

					public void OnClick()
					{
						String str = menus[m_relive_type][m_relive_relive.getResult() - 1];
						if (str.equals("原地复活"))
						{
							open(RELIVE_BUY);
						}
						else if (str.equals("回城复活"))
						{
							DWGameManager.getInstance().getSendMessage().sendReliveRoleBackCity();
						}
					}
				});
				break;
			case RELIVE_BUY:
				m_relive_relive = new DWMessageBox(title, message[m_relive_state] + m_relive_gold
						+ message[m_relive_state + 1], "确定", "取消");
				m_relive_relive.addListener(new DWListener() {
					public void OnDoubleClick()
					{
					}

					public void OnClick()
					{
						switch (m_relive_relive.getResult()) {
							case DWMessageBox.SOFTKEY_OK:
								if(DWGameManager.getInstance().m_role.m_rmb>=m_relive_gold)
									DWGameManager.getInstance().getSendMessage().sendReliveRole();
								else
								{
									DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "元宝不足~");
									open(RELIVE_NORMAL);
								}	
								break;
							case DWMessageBox.SOFTKEY_BACK:
								open(RELIVE_NORMAL);
								break;
						}
					}
				});
				break;
			case RELIVE_OTHER:
				m_relive_relive = new DWMessageBox(title, m_relive_name + message[m_relive_state], "确定", "取消");
				m_relive_relive.addListener(new DWListener() {
					public void OnDoubleClick()
					{
					}

					public void OnClick()
					{
						switch (m_relive_relive.getResult()) {
							case DWMessageBox.SOFTKEY_OK:
								DWGameManager.getInstance().getSendMessage().sendReliveRoleBackCity();
								break;
							case DWMessageBox.SOFTKEY_BACK:
								open(RELIVE_NORMAL);
								break;
						}
					}
				});
				break;
		}
		DWControlsManager.getInstance().addControl(m_relive_relive);
	}

	public void close(byte index)
	{
		if (DWControlsManager.getInstance().contains(m_relive_relive))
			DWControlsManager.getInstance().removeControl(m_relive_relive);
		m_relive_relive = null;
	}

	public void dispose()
	{
	}

	public void logic()
	{
	}

	public void recvRoleDead(DataInputStream dis) throws IOException
	{
		try {
			DWGameManager.getInstance().m_role.m_status = Role.ACTOR_STATUS_DEAD;
			m_relive_message=dis.readUTF();
			Tools.debugPrintln("死亡信息：" + m_relive_message);
			m_relive_type = dis.readByte();
			Tools.debugPrintln("死亡类型：" + m_relive_type);
			m_relive_gold = dis.readInt();
			Tools.debugPrintln("复活花费" + m_relive_gold);
			if(m_relive_type!=2){
				open(RELIVE_NORMAL);
			}			
		}
		catch (Exception e) {
			Tools.debug(e);
		}
	}

	public void recvRoleOtherRelive(DataInputStream dis) throws IOException
	{
		try {
			m_relive_name = dis.readUTF();
			open(RELIVE_OTHER);
		}
		catch (Exception e) {
			Tools.debug(e);
		}
	}

	public void recvRoleRliveStates(DataInputStream dis) throws IOException
	{
		close((byte) 0);

		try {
			int states = dis.readShort();
			String message = dis.readUTF();
			DWGameManager.getInstance().m_role.m_hp = dis.readInt();
			if (UIWindows.getMain() != null) {
				UIWindows.getMain().updateMyHead();
			}

			if (states == 1)
			{
				DWGameManager.getInstance().m_role.m_status = Role.ACTOR_STATUS_FREE;
			}

			DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, message);
		}
		catch (Exception e) {
			Tools.debug(e);
		}
	}

}
